package com.anlu.concurrent.ch2.tools.semaphone;

import sun.awt.image.ImageWatched;

import java.sql.Connection;
import java.util.LinkedList;
import java.util.concurrent.Semaphore;

public class DBPoolSemaphore {
    private final static  int POOL_SIZE = 10;
    /**
     * useful表示可用的数据库连接，useless表示已用的数据库连接
     */
    private final Semaphore useful,useless;
    public DBPoolSemaphore() {
        this.useful = new Semaphore(POOL_SIZE);
        this.useless = new Semaphore(0);
    }

    //存放数据库连接的容器
    private static LinkedList<Connection> pool = new LinkedList<>();
    //初始化池
    static {
        for (int i = 0; i < POOL_SIZE; i++) {
            pool.addLast(SqlConnectImpl.fetchConnection());
        }
    }

    /**
     * 归还连接
     * @param connection
     * @throws InterruptedException
     */
    public void returnConnect(Connection connection)throws InterruptedException{
        if(connection!=null){
            System.out.println("当前有"+useful.getQueueLength()+"个线程等待数据库连接！！"
                    +"可用连接数:"+useful.availablePermits());
            useless.acquire();
            synchronized (pool){
                pool.addLast(connection);
            }
            useful.release();
        }
    }

    /**
     * 从池子中拿连接
     * @return
     * @throws InterruptedException
     */
    public Connection takeConnect()throws InterruptedException{
        useful.acquire();
        Connection connection;
        synchronized (pool){
            connection = pool.removeFirst();
        }
        useless.release();
        return connection;
    }

}
